package com.cn.qgx428.orderingmeal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.Store;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.Shop;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by QinX on 2017/4/4.
 */

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopViewHolder> {

    private Context context;
    private List<Shop> shops;

    private MyKeyInfo keyInfo;

    public ShopAdapter(Context context, List<Shop> shops, MyKeyInfo keyInfo) {
        this.context = context;
        this.shops = shops;
        this.keyInfo = keyInfo;
    }

    @Override
    public ShopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.food_shop_item, parent, false);
        ShopViewHolder shopViewHolder = new ShopViewHolder(itemView);
        return shopViewHolder;
    }

    @Override
    public void onBindViewHolder(final ShopViewHolder holder, final int position) {
        holder.shopName.setText(shops.get(position).getName());
        holder.shopAdress.setText(shops.get(position).getAddress());
        Picasso.with(context).load(shops.get(position).getImg().getFileUrl()).into(holder.shopBG);
        holder.shopLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, Store.class);
                keyInfo.setNowShopID(shops.get(position).getObjectId());
                keyInfo.setNowShopName(shops.get(position).getName());
                keyInfo.setNowShopAddress(shops.get(position).getAddress());
                keyInfo.setNowPicUrl(shops.get(position).getImg().getFileUrl());
                keyInfo.setShopGrade(shops.get(position).getGrade());
                keyInfo.setShopPhone(shops.get(position).getPhone());
                intent.putExtra("keyInfo", keyInfo);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return shops.size();
    }

    public void addMoreData(List<Shop> shops) {
        this.shops.addAll(shops);
        notifyDataSetChanged();
    }

    public class ShopViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout shopLayout;
        private ImageView shopBG;
        private TextView shopName;
        private TextView shopAdress;

        public ShopViewHolder(View itemView) {
            super(itemView);
            shopLayout = (LinearLayout) itemView.findViewById(R.id.food_shop_itemLinaerLayout);
            shopBG = (ImageView) itemView.findViewById(R.id.food_shop_img);
            shopName = (TextView) itemView.findViewById(R.id.food_shop_name);
            shopAdress = (TextView) itemView.findViewById(R.id.food_shop_address);
        }
    }
}
